/*
 * @Author: 李琪 liqi@lark2game.com
 * @Date: 2024-12-07 10:57:20
 * @LastEditors: 李琪 liqi@lark2game.com
 * @LastEditTime: 2024-12-10 15:52:13
 * @FilePath: /cramped-room/assets/Script/Manager/BattleManager.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { _decorator, Component, Node } from 'cc'
import { TileMapManager } from './TileMapManager'
import { createUINode } from '../Utils/UtilsIndex'
import Levels, { ILevel } from '../Config/Levels/LevelsIndex'
import GameData, { DataMgr } from '../Runtime/GameData'
import EventManager from '../Runtime/EventManager'
import { GAME_EVENT_ENUM } from '../Config/Enums/EnumsIndex'
import { PlayerManager } from './PlayerManager'
import ResourceManager from '../Runtime/ResourceManager'
const { ccclass } = _decorator

@ccclass('BattleManager')
export class BattleManager extends Component {
  level: ILevel
  stageNode: Node
  protected onLoad() {
    EventManager.Instance.on(GAME_EVENT_ENUM.ChangeLevel, this.ChangeLevel, this)
  }
  ChangeLevel() {
    this.clearTileMap()
    let curLevel = GameData.Instance.curLevel + 1
    if (curLevel > GameData.Instance.totalNumLevel) curLevel = 1
    GameData.Instance.curLevel = curLevel

    this.initLevel()
  }
  async start(): Promise<void> {
    await ResourceManager.Instance.init()
    this.generateStage()
    //生成瓦片地图
    this.initLevel()

    // 初始化生成玩家
    this.generatePlayer()
  }
  generatePlayer() {
    //玩家节点
    const playerNode = createUINode('playerNode')
    this.stageNode.addChild(playerNode)
    playerNode.setPosition(0, 0)

    const playerManager = playerNode.addComponent(PlayerManager)
    playerManager.init()
  }
  generateStage() {
    //舞台节点
    this.stageNode = createUINode('stageNode')
    this.node.addChild(this.stageNode)
  }
  initLevel() {
    const level = Levels[`Level${GameData.Instance.curLevel}`]
    if (level) {
      this.level = level
      GameData.Instance.mapInfo = this.level.mapInfo //存到数据中心
      GameData.Instance.mapRowCount = this.level.mapInfo.length || 0 //存到数据中心
      GameData.Instance.mapColumnCount = this.level.mapInfo[0].length || 0 //存到数据中心
      this.generateTileMap()
    }
  }
  generateTileMap() {
    const tileMapNode = createUINode('tileMapNode')
    this.stageNode.addChild(tileMapNode)

    const tileManager = tileMapNode.addComponent(TileMapManager)
    tileManager.init()

    //地图左上角加载，适配屏幕，居中
    this.adaptPos()
  }
  //清空tileMap
  clearTileMap() {
    if (this.stageNode.children) this.stageNode.destroyAllChildren()
  }
  adaptPos() {
    //从右下角生成的舞台
    const { mapRowCount, mapColumnCount } = GameData.Instance
    const disX = (DataMgr.TILE_WIDTH * mapRowCount) / 2
    const disY = (DataMgr.TILE_HEIGHT * mapColumnCount) / 2 + 80

    this.stageNode.setPosition(-disX, disY)
  }
  protected onDestroy(): void {
    EventManager.Instance.off(GAME_EVENT_ENUM.ChangeLevel, this.ChangeLevel)
    this.clearTileMap()
  }

  update(deltaTime: number) {}
}
